﻿using Azure.Core;
using DataModels.Dto;
using DataModels.Dto.Livelihood;
using DataModels.Entities.Base;
using DataModels.Entities.Community;
using DataModels.Entities.Livelihood;
using DataModels.Entities.ServiceDay;
using DataModels.Enums.Livelihood;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Utils;
using static DataModels.Enums.GeneralEnum;
using static QRCoder.PayloadGenerator;

namespace Services.Livelihood
{
    public class HouseRentSaleService(SqlSugarClient db) : BaseService(db)
    {
        /// <summary>
        /// 获取房屋租售列表 add by tianjh 20250808
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public ResponseModel GetHouseRentSaleList(HouseRentSalePageRequest request)
        {
            var total = 0;

            if (request == null)
                return Error("缺失参数");

            var rentCount = db.Queryable<HouseRentSale>()
            .Where(x => x.Status == Status.Enable)
            .Where(x => x.ProjectUUID == request.ProjectUUID.ToString())
            .Where(x => x.Type == HouseRentSaleType.OnRent)
            .Count();

            var saleCount = db.Queryable<HouseRentSale>()
            .Where(x => x.Status == Status.Enable)
            .Where(x => x.ProjectUUID == request.ProjectUUID.ToString())
            .Where(x => x.Type == HouseRentSaleType.OnSale)
            .Count();

            var list = db.Queryable<HouseRentSale>()
            .Where(x => x.Status == Status.Enable)
            .WhereIF(request.ProjectUUID.HasValue, x => x.ProjectUUID == request.ProjectUUID.ToString())
            .WhereIF(request.Type.HasValue, x => x.Type == request.Type.Value)
            .OrderByDescending(x => x.CreateTime)
            .ToPageList(request.PageNumber, request.PageSize, ref total);

            return Success(new
            {
                RentCount = rentCount,    // 出租数量
                SaleCount = saleCount,    // 出售数量
                List = list,              // 分页数据列表
                Total = total             // 总记录数（可选，前端可能需要）
            });
        }

        /// <summary>
        /// 获取房屋租售的小区列表 add by tianjh 20250811
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public ResponseModel GetHouseRentSaleProjectList()
        {
            var list = db.Queryable<HouseRentSale, Projects>((hrs, p) => new object[]
            {
                JoinType.Left,hrs.ProjectUUID == p.UUID.ToString()
            })
            .Where(hrs => hrs.Status == Status.Enable)
            .Select((hrs, p) => new
            {
                ProjectUUID = hrs.ProjectUUID,              
                ProjectName = p.ProjectName
            })
            .Distinct()
            .ToList();

            return Success(list);
        }

        /// <summary>
        /// 房屋租售预约需求 绑定下拉框用
        /// </summary>
        /// <returns></returns>
        public ResponseModel GetHouseRentSaleDemandList()
        {
            var list = new List<object>();
            var enumValues = Enum.GetValues(typeof(HouseRentSaleDemand));

            foreach (HouseRentSaleDemand value in enumValues)
            {
                MemberInfo[] memberInfo = typeof(HouseRentSaleDemand).GetMember(value.ToString());
                if (memberInfo.Length > 0)
                {
                    var descriptionAttr = memberInfo[0].GetCustomAttribute<DescriptionAttribute>();
                    string description = descriptionAttr?.Description ?? value.ToString();

                    list.Add(new
                    {
                        Value = (int)value,          // 枚举数值
                        Name = value.ToString(),     // 枚举成员名
                        Description = description    // 描述信息
                    });
                }
            }

            return Success(list);
        }

        /// <summary>
        /// 一键预约
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public ResponseModel OneKeyReservation(HouseRentSaleReservationInfo request)
        {
            if (request == null)
                return Error("缺失参数");

            var entity = new HouseRentSaleReservation();
            entity.UUID = Guid.NewGuid().ToString();
            if (!string.IsNullOrEmpty(request.HouseRentSaleUUID))
                entity.HouseRentSaleUUID = request.HouseRentSaleUUID; //房屋租售表外键
            entity.UserUUID = UserUUID.ToString(); //预约用户UUID 取当前登录人
            entity.UserName = UserName; //预约用户名称 取当前登录人
            entity.Demand = request.Demand; //预约需求      
            if (!string.IsNullOrEmpty(request.HouseUUID))
                entity.HouseUUID = request.HouseUUID; //预约房屋UUID
            entity.HouseName = request.HouseName; //房源名称
            entity.Phone = request.Phone;  //电话
            entity.CreateUser = UserUUID.ToString(); //创建人 取当前登录人
            entity.CreateTime = DateTime.Now; //创建时间 取当前时间

            db.Insertable(entity).ExecuteCommand();

            return Success("一键预约成功");
        }


    }
}
